<?php

namespace app\admin\model;

use think\Db;
use think\Model;

class BaseModel extends Model
{
    //查询存储过程数据
    public function procedure_data_select($params=[],$must_param=[],$procedure_name='',$out_column=[],$in_out_column=[])
    {
        $list = [];
        if($params && $must_param && $procedure_name){

            $set = preDealProcedureParam($params,$must_param,$out_column);
            my_debug(1,'$set--43');
            my_debug(1,$set);
            if($in_out_column){
                $sql =  dealProcedureInOutColumn($in_out_column,$params);
                Db::query($sql);
            }
            $sql = "call {$procedure_name}($set)";

            my_debug(1,'$sql--47');
            my_debug(1,$sql);

            $list = Db::query($sql);
            if(is_array($list) && count($list)){
                return $list;
            }
            if($out_column){
                $rs_out = dealProcedureOutColumn($out_column);
                $sql = "select {$rs_out}";
            }else{
                $sql = "select @_res";
            }
            my_debug(1,'$sql--60');
            my_debug(1,$sql);

            $list = Db::query($sql);
            my_debug(1,'$list--64');
            my_debug(1,$list);
            $list = dealWithProcedureColumn($list,$must_param);
            my_debug(1,'$list--67');
            my_debug(1,$list);
        }
        return $list;
    }

    //添加存储过程数据
    public function procedure_data_add($params=[],$must_param=[],$procedure_name='')
    {
        $rs = [];
        if($params && $must_param && $procedure_name){
            $set = preDealProcedureParam($params,$must_param);
            $sql = "call {$procedure_name}($set)";

            my_debug(1,'$sql--81');
            my_debug(1,$sql);
            $rs = Db::query($sql);

            my_debug(1,'$rs--85');
            my_debug(1,$rs);

            $sql = "select {$set}";
            my_debug(1,'$sql--89');
            my_debug(1,$sql);
            $rs = Db::query($sql);

            my_debug(1,'$rs--93');
            my_debug(1,$rs);
            $rs = dealWithProcedureColumn($rs,$must_param);
            if(count($rs) == 1){
                $rs = $rs[0];
            }
        }
        return $rs;
    }
}